package com.sttg.csp.common.util;

import java.io.InputStream;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.InitialLdapContext;

public final class LDAPUtil {
	
	
	private static DirContext ctx;
	
	private static Hashtable<String, String> env = new Hashtable<String, String>();
	
	private static Properties prop = new Properties();
	
	static {
		try {
			// read configure file config.properties
			InputStream in = LDAPUtil.class.getClassLoader().getResourceAsStream("config.properties");
			prop.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// URL of LDAP Server
	public final static String LDAP_URL = (String) prop.get("ldap.url");

	// Base DN of LDAP Server
	public final static String BASE_DN = (String) prop.getProperty("ldap.baseDN");
	
	// User DN for managing LDAP
	public final static String MANAGER_DN = (String) prop.getProperty("ldap.managerDN");
	
	// User's password for managing LDAP
	public final static String MANAGER_PASSWORD = (String) prop.getProperty("ldap.managerPassword");

	// Authentication type
	public final static String AUTH_TYPE = (String) prop.getProperty("ldap.authType");

	public final static String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
	
	// RDP of BD user
	public final static String BD_USER_RDP = (String) prop.getProperty("ldap.bdUserRDP");
	
	// RDP of BD admin
	public final static String BD_ADMIN_RDP = (String) prop.getProperty("ldap.bdAdminRDP");
	
	// RDP of Certificate association
	public final static String BD_CER_ASSOC_RDP = (String) prop.getProperty("ldap.bdCerAssocRDP");
	
	static {
		setEnvironment();
	}
	
	// initial LDAP for connection 
	public LDAPUtil() {}

	// set info for login ldap server.
	private static void setEnvironment() {
		env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
		env.put(Context.PROVIDER_URL, LDAP_URL);
		env.put(Context.SECURITY_AUTHENTICATION, AUTH_TYPE);
		env.put(Context.SECURITY_PRINCIPAL, MANAGER_DN);
		env.put(Context.SECURITY_CREDENTIALS, MANAGER_PASSWORD);
		env.put("com.sun.jndi.ldap.connect.timeout","3000");  
	}

	public static DirContext getDirContext(){  
        try {  
            ctx = new InitialLdapContext(env, null);  
        } catch (NamingException ex) {  
            ex.printStackTrace();  
        }  
        return ctx;  
    }  
	
	// close ldap connection
	public static void closeConnection() {
		try {
			ctx.close();
		} catch (NamingException ne) {
			ne.printStackTrace();
		}
	}
	
	public static void putAttribute(Attributes attrs, String attrName, String attrValue) {
		if (attrValue != null && attrValue.length() != 0) {
			Attribute attr = new BasicAttribute(attrName, attrValue);
			attrs.put(attr);
		}
	}
	
	public static String getAttribute(Attributes attrs, String attrName) throws NamingException {
		Attribute attr = attrs.get(attrName);
		if (attr == null) {
			return "";
		} else {
			return (String) attr.get();
		}
	}

	public static void main(String[] args) {

		try {
			DirContext ctx = getDirContext();
			String user = "orcladmin";
			
			Attributes attrs = ctx.getAttributes("cn=" + user + ",cn=users," + BASE_DN);
			
			System.out.println("First Name: " + getAttribute(attrs, "givenname"));
			System.out.println("Last Name: " + getAttribute(attrs, "sn"));
			System.out.println("Common Name: " + getAttribute(attrs, "cn"));
			System.out.println("User ID: " + getAttribute(attrs, "uid"));
			System.out.println("E-Mail: " + getAttribute(attrs, "mail"));
			System.out.println("Phone: " + getAttribute(attrs, "telephonenumber"));
			System.out.println("Fax: " + getAttribute(attrs, "facsimiletelephonenumber"));
			System.out.println("List completed.");
			System.out.println("-----------------------------n");
			
		} catch (Exception e) {
			System.out.println(e);
		}  finally {
			closeConnection();
		}
	}
}